using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using WindowsGameLibrary1;

namespace Bipoland
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Color color;
        List<Entity> entitysWhite;
        List<Entity> entitysBlack;
        Shift shift;
        KeyboardState state;
        Dictionary<ModeEnum, bool> flagCollections;
        public static Belmont belmont;
        bool isAttacking;
        float attackTime;
        List<BackGround> layers1;
        List<BackGround> layers2;
        Scrolling scrolling;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;

            Content.RootDirectory = "Content";
            color = new Color();
            entitysWhite = new List<Entity>();
            entitysBlack = new List<Entity>();
            Ratita ratita = new Ratita(Shift.White);
            ratita.Position1 = new Vector2(200, 500);
            entitysWhite.Add(ratita);
            Ratita ratita2 = new Ratita(Shift.Black);
            ratita2.Position1 = new Vector2(700, 500);
            entitysBlack.Add(ratita2);
            shift = Shift.White;
            flagCollections = new Dictionary<ModeEnum, bool>();
            bool hit=false;
            flagCollections.Add(ModeEnum.One, hit);
            belmont = new Belmont(graphics.PreferredBackBufferWidth);
            isAttacking = false;
            attackTime = 0;
            isAttacking = false;

            CreateBackGround();

            scrolling = Scrolling.getInstance();

            
            
        }

        private void CreateBackGround()
        {
            layers1 = new List<BackGround>();
            layers2 = new List<BackGround>();
            int i = 0;
            BackGround f1 = new BackGround(35f, "GUGASBACKGROUND/fond1") { Position1 = new Vector2(1024 * i++,0) };            
            BackGround f2 = new BackGround(35f, "GUGASBACKGROUND/Fond2"){ Position1 = new Vector2(1024 * i++,0) };
            BackGround f3 = new BackGround(35f, "GUGASBACKGROUND/fond1"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround f4 = new BackGround(35f, "GUGASBACKGROUND/Fond2"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround f5 = new BackGround(35f, "GUGASBACKGROUND/fond1"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround f6 = new BackGround(35f, "GUGASBACKGROUND/Fond2"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround f7 = new BackGround(35f, "GUGASBACKGROUND/fond1"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround f8 = new BackGround(35f, "GUGASBACKGROUND/Fond2"){ Position1 = new Vector2((1024 * i++),0) };
            i = 0;
            BackGround m1 = new BackGround(20f, "GUGASBACKGROUND/mont1"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround m2 = new BackGround(20f, "GUGASBACKGROUND/mont2"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround m3 = new BackGround(20f, "GUGASBACKGROUND/mont1"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround m4 = new BackGround(20f, "GUGASBACKGROUND/mont2"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround m5 = new BackGround(20f, "GUGASBACKGROUND/mont1"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround m6 = new BackGround(20f, "GUGASBACKGROUND/mont2"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround m7 = new BackGround(20f, "GUGASBACKGROUND/mont1"){ Position1 = new Vector2((1024 * i++),0) };
            BackGround m8 = new BackGround(20f, "GUGASBACKGROUND/mont2"){ Position1 = new Vector2((1024 * i++),0) };

            layers1.Add(f1);
            layers1.Add(f2);
            layers1.Add(f3);
            layers1.Add(f4);
            layers1.Add(f5);
            layers1.Add(f6);
            layers1.Add(f7);
            layers1.Add(f8);
            
            layers2.Add(m1);
            layers2.Add(m2);
            layers2.Add(m3);
            layers2.Add(m4);
            layers2.Add(m5);
            layers2.Add(m6);
            layers2.Add(m7);
            layers2.Add(m8);
            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            belmont.LoadContent(Content);
            foreach (BackGround b in layers1)
            {
                b.LoadContent(Content);
            }
            foreach (BackGround b in layers2)
            {
                b.LoadContent(Content);
            }
            for (int i = 0; i < entitysBlack.Count; i++)
            {
                entitysBlack[i].LoadContent(Content);
                
            }
            for (int i = 0; i < entitysWhite.Count; i++)
            {
                entitysWhite[i].LoadContent(Content);

            }
            entitysBlack.Add(belmont);
            entitysWhite.Add(belmont);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            state = Keyboard.GetState();
            ShiftReality();
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            belmontMove(gameTime);
            foreach (BackGround b in layers1)
            {
                b.Update(gameTime);
            }
            foreach (BackGround b in layers2)
            {
                b.Update(gameTime);
            }
            if (shift.Equals(Shift.White))
            {
                for (int i = 0; i < entitysWhite.Count; i++)
                {
                    entitysWhite[i].Update(gameTime);

                }    
            }
            else
            {
                for (int i = 0; i < entitysBlack.Count; i++)
                {
                    entitysBlack[i].Update(gameTime);

                }
            
            }
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            // TODO: Add your drawing code here
            
            //foreach (BackGround b in layers2)
            //{
            //    b.Draw(spriteBatch, gameTime, 0, Scrolling.getInstance().Scrolling1.X);
            //}
            foreach (BackGround b in layers1)
            {
                b.Draw(spriteBatch, gameTime, 0, Scrolling.getInstance().Scrolling1.X);
            }
            if (shift.Equals(Shift.White))
            {
                //backGrounds.ChangeWhite();
                for (int i = 0; i < entitysWhite.Count; i++)
                {
                    entitysWhite[i].Draw(spriteBatch,gameTime,0,Scrolling.getInstance().Scrolling1.X);

                }
            }
            else
            {
                //backGrounds.ChangeBlack();
                for (int i = 0; i < entitysBlack.Count; i++)
                {
                    entitysBlack[i].Draw(spriteBatch, gameTime, 0, Scrolling.getInstance().Scrolling1.X);
                }

            }
            
            spriteBatch.End();
            base.Draw(gameTime);
        }

        public void ShiftReality()
        {
            
            if (HitShiftKey())
            {
                if (shift.Equals(Shift.White)) shift = Shift.Black;
                
                else shift = Shift.White;
                
            }
            
        }
        public bool HitShiftKey()
        {
            
            bool keyDown = flagCollections[ModeEnum.One];


            if (state.IsKeyDown(Keys.A) && !keyDown)
            {
                keyDown = true;
                flagCollections[ModeEnum.One] = keyDown;
                return state.IsKeyDown(Keys.A);
            }
            else
            {
                if (state.IsKeyUp(Keys.A) && keyDown)
                {
                    keyDown = false;
                    flagCollections[ModeEnum.One] = keyDown;
                }
            }
            return false;
        }

        public void belmontMove(GameTime gameTime)
        {
            belmont.DirectionX = 0;
            bool keyDown = false;
            if (!isAttacking)
            {


                if (state.IsKeyDown(Keys.Right))
                {
                    belmont.Orientation = 1;
                    belmont.DirectionX = 1;
                    belmont.WalkingAnimation();
                    keyDown = true;
                    foreach (BackGround b in layers1)
                    {
                        b.direction = -1;
                    }
                    foreach (BackGround b in layers2)
                    {
                        b.direction = -1;
                    }
                }
                if (state.IsKeyDown(Keys.Left))
                {
                    belmont.Orientation = -1;
                    belmont.DirectionX = -1;
                    belmont.WalkingAnimation();
                    keyDown = true;
                    foreach (BackGround b in layers1)
                    {
                        b.direction = 1;
                    }
                    foreach (BackGround b in layers2)
                    {
                        b.direction = 1;
                    }
                }
                if (state.IsKeyDown(Keys.Down))
                {
                    belmont.crouchAnimation();
                    keyDown = true;
                }
                if (state.IsKeyDown(Keys.Space))
                {
                    belmont.attackingAnimation();
                    keyDown = true;
                    isAttacking = true;
                }
                if (!keyDown)
                {
                    belmont.StandUpAnimation();
                }

            }
            else
            {
                attackTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (attackTime > belmont.TotalAnimationTime())
                {
                    attackTime = 0;
                    isAttacking = false;
                }
            }
        }

    }

    public enum Shift 
    {
        White,
        Black
    }
}
